package cn.xm.lingshi.dao.impl;

import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

import cn.xm.lingshi.dao.OrderDao;
import cn.xm.lingshi.pojo.DingDan;
import cn.xm.lingshi.pojo.GouWuChe;
import cn.xm.lingshi.pojo.Shop;
import cn.xm.lingshi.utlis.BaseDao;

public class OrderDaoImpl extends BaseDao implements OrderDao {

	@Override
	public List<DingDan> queryRecord(int offer, int pageSize, int userid,
			int type) throws Exception {
		List<DingDan> list = new ArrayList<DingDan>();
		StringBuffer preparedSql = new StringBuffer();
		List<Object> listParams = new ArrayList<Object>();
		preparedSql
				.append(" SELECT orderid,address,amount FROM `order` WHERE userid=? AND ordertype=? ");
		listParams.add(userid);
		listParams.add(type);
		preparedSql.append("ORDER BY orderid LIMIT ?,? ");
		listParams.add(offer);
		listParams.add(pageSize);
		Object[] params = listParams.toArray();
		ResultSet rs = super.executeQuery(preparedSql.toString(), params);
		StringBuffer sql = new StringBuffer();
		sql.append("SELECT shopnum,shopname,shopimg,shoporice,shopid,kucun FROM  orderdetail ");
		sql.append(" INNER JOIN `shop` ON `shop`.id=`orderdetail`.shopid  WHERE orderid=?");
		
		while (rs != null && rs.next()) {
			DingDan order = new DingDan();
			int id=rs.getInt("orderid");
			order.setOrderid(id);
			order.setAddress(rs.getString("address"));
			order.setAmount(rs.getDouble("amount"));
			// 读取数据库，将订单id为1的商品
			Object[] param = null;
			if(rs.getString("orderid")!=null&&rs.getString("orderid")!=""){
				param=new Object[]{id};			
			}
			ResultSet set = super.executeQuery(sql.toString(), param);
			List<Shop> arrList=new ArrayList<Shop>();
			while (set != null && set.next()) {
				Shop shop=new Shop();
				shop.setKucun(set.getInt(1));
				shop.setShopname(set.getString(2));
				shop.setShopimg(set.getString(3));
				shop.setShoporice(set.getInt(4));
				shop.setId(set.getInt(5));
				shop.setShopnum(set.getInt(6));
				arrList.add(shop);
				
			}
			order.setList(arrList);
			list.add(order);
		}
		return list;
	}

	@Override
	public List<Shop> queryShopByorder(int orderid) throws Exception {
		String sql = "SELECT shopid,shopnum FROM `orderdetail` WHERE orderid=1";

		return null;
	}
	

	@Override
	public int addOrder(GouWuChe cart,String address) throws Exception {
		String sql="INSERT INTO `order` (userid,address,ordertype,amount) VALUES (?,?,1,?)";
		Object[] params={cart.getUserid(),address,cart.getMoney()};
		return super.executeUpdate(sql, params);
	}

	@Override
	public int addOrderdetail(int orderid,int shopid,int shopnum) throws Exception {
		String sql="INSERT INTO `orderdetail` (orderid,shopid,shopnum) VALUES (?,?,?)";
		Object[] params={orderid, shopid, shopnum};
		return super.executeUpdate(sql, params);
	}

	@Override
	public int queryOrderidByUserid(int userid) throws Exception {
		String sql="SELECT orderid FROM `order` WHERE userid=? ORDER BY gdate DESC LIMIT 0,1";
		Object[] params={userid};
		ResultSet rs=super.executeQuery(sql, params);
		if(rs!=null&&rs.next()){
			return rs.getInt(1);
		}
		super.closeResource(rs);
		return 1;
	}

	@Override
	public int queryKuCunByShopid(int shopid, int kuncun) throws Exception {
		String sql="UPDATE `shop` SET `kucun` = ? WHERE id= ?";
		Object[] params={kuncun,shopid};
		return super.executeUpdate(sql, params);
	}
}
